// pages/message/index.js
import {
  ApiGetMessageList
} from '../../api/index'
const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload,
  webData
} = app.globalData
import {
  formatDate,
  debounce
} from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    setDA: webData,
    baseUrl,
    commonUpdate,
    commonUpload,
    showpopup: false,
    loading: false,
    params: {
      page: 1,
      size: '10',
      finish: false,
      search_data: ''
    },
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    /*   console.log(app.globalData)
      this.setData({
        webdata: wx.getStorageSync('webdata')
      }) */
  },
  //切换就诊人
  /*  getChangeData(e) {

     var user = wx.getStorageSync('userInfo');
     user.consultation_uid = e.detail.value
     wx.setStorageSync('userInfo', user)
     this.getMessageList()
   }, */
  // 切换就诊人
  exChangeClick() {
    this.setData({
      showpopup: true
    })
  },
  //点击消息
  messageClick(e) {
    const item = e.currentTarget.dataset.v
    wx.setStorageSync('comment', item.comment)
    /*    if (item.statusS == '未开始') {
         wx.showModal({
           title: '提示',
           content: '当前暂无医生接诊，请等待接诊后进入问诊间',
           icon: 'error',
           showCancel: false
         })
       } else { */
    const ops = {
      icon: item.icon,
      id: item.id,
      doctor_id: item.doctor_id,
      doctor_name: item.doctor_name,
      department_name: item.department_name,
      consultation_order_id: item.consultation_order_id,
      status: item.status,
      finish_time: item.finish_time,
      comment: item.comment,
      statusS: item.statusS,
      order_time: item.order_time,
      status: item.status,
      finish_time: item.finish_time,
      cancel_time: item.cancel_time
    }

    wx.setStorageSync('opsPatient', ops)
    wx.navigateTo({
      url: `/pages/messageDetail/index?contact=${JSON.stringify(ops)}`,
    })
    /*  } */



  },

  onShow() {
    this.getTabBar().init();
    this.getMessageList()
    /*     console.log(app.ws.listPage(true)) */
    app.ws.onList(true, this.refreshList.bind(this))
  },
  //更新消息列表
  refreshList() {
    let val = wx.getStorageSync('webData')
    /*  if (val.type === 'img') {
       val.content = "[图片]"
     } */
    // 更新信息
    let dataInfo = this.data.messageArr.findIndex(item => {
      return item.consultation_order_id == val.consultation_order_id
    })
    this.data.messageArr[dataInfo].last_message = val.content
    this.data.messageArr[dataInfo].patient_notice++
    this.data.messageArr[dataInfo].type = val.type
    this.data.messageArr.unshift(this.data.messageArr.splice(dataInfo, 1)[0])
    this.setData({
      messageArr: this.data.messageArr
    })



    /*     this.getMessageList() */
  },



  /*   findLeftDataIndex(webId) {
      return this.data.messageArr[0].findIndex(item => item.id === webId)
    }, */
  formatText(status) {
    let val = this.isCurrentTimeInRange(status)
    if (val) {
      return '未开始'
    }
    /*  console.log('statusstatusstatus', status)
     if (status == '1') {
       return '待接诊'
     } else if (status == '3') {
       return '已结束'
     } */
  },
  //判断当前时间是否再字符串内
  isCurrentTimeInRange(timeRangeStr) {
    // 解析输入的时间范围字符串 '2024-12-04 13:47-15:00'
    const [dateStr, timeRange] = timeRangeStr.split(' '); // 拆分日期和时间范围

    // 拆分开始时间和结束时间
    const [startTimeStr, endTimeStr] = timeRange.split('-');

    // 构造完整的日期时间字符串并转为 Date 对象
    const startDateTime = new Date(`${dateStr}T${startTimeStr}:00`);
    const endDateTime = new Date(`${dateStr}T${endTimeStr}:00`);

    // 获取当前时间
    const currentTime = new Date();

    // 判断当前时间是否在范围内
    return currentTime < startDateTime
  },



  getMessageList() {
    /*     let id = wx.getStorageSync('userInfo').id */
    let that = this
    wx.showLoading({
      title: '加载中'
    })
    ApiGetMessageList(this.data.params).then(res => {
      const data = res.data.map(item => {
        item.last_time = formatDate(item.last_time);
        !item.order_time ? item.statusS = item.statusS : item.statusS = that.formatText(item.order_time)
        /*   item.statusS = that.formatText(item.order_time) */
        /*    if (item.type === 'img') {
             item.last_message = "[图片]"
           } */
        return item
      })
      this.setData({
        messageArr: data
      })
      wx.hideLoading()
    })
  },

  onClose() {
    this.setData({
      showpopup: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})